<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
<!--    "dataForm"&ndash;&gt;{{dataForm}}-->
      <el-form-item label="分类" prop="category">
      <el-input v-model="dataForm.category" placeholder="分类"></el-input>
    </el-form-item>
    <el-form-item label="饮品名" prop="dName">
      <el-input v-model="dataForm.dname" placeholder="饮品名"></el-input>
    </el-form-item>
    <el-form-item label="冷热" prop="hotCold">
      <el-input v-model="dataForm.hotCold" placeholder="冷热"></el-input>
    </el-form-item>
    <el-form-item label="描述" prop="descri">
      <el-input v-model="dataForm.descri" placeholder="描述"></el-input>
    </el-form-item>
    <el-form-item label="价格" prop="price">
      <el-input v-model="dataForm.price" placeholder="价格"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-switch
          v-model="dataForm.status"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="1"
          :inactive-value="0">
      </el-switch>
    </el-form-item>
    <el-form-item label="图片" prop="dImg">
      <el-input v-model="dataForm.dimg" placeholder="图片"></el-input>
    </el-form-item>
<!--    <el-form-item label="" prop="isDeleted">-->
<!--      <el-input v-model="dataForm.isDeleted" placeholder=""></el-input>-->
<!--    </el-form-item>-->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          category: '',
          dName: '',
          hotCold: '',
          descri: '',
          price: '',
          status: '',
          dImg: '',
          isDeleted: ''
        },
        dataRule: {
          category: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          dname: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          hotCold: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          descri: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$axios({
              url: `/pcDrink/info/${this.dataForm.id}`,
              method: 'get',

            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm = data.pcDrink
                // this.dataForm.category = data.pcDrink.category
                // this.dataForm.dName = data.pcDrink.dName
                // this.dataForm.hotCold = data.pcDrink.hotCold
                // this.dataForm.descri = data.pcDrink.descri
                // this.dataForm.price = data.pcDrink.price
                // this.dataForm.status = data.pcDrink.status
                // this.dataForm.dImg = data.pcDrink.dImg
                // this.dataForm.isDeleted = data.pcDrink.isDeleted
                console.log("data->",data.pcDrink)
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            console.log("this.dataForm ---->",this.dataForm)
            this.$axios({
              url: `/pcDrink/${!this.dataForm.id ? 'save' : 'update'}`,
              method: `${this.dataForm.id ? 'put':'post'}`,
              data: this.dataForm
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
